package com.panda.game.xls.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.panda.game.xls.constant.ScreenConstant;

import java.util.ArrayList;
import java.util.List;

public class SheetTypeDataConfig extends IOFileLoadRunner {

    private static List<SheetTypeData> sheetTypeDataList = null;

    //初始化入口
    private static void init() {
        sheetTypeDataList = new ArrayList();
        JSONObject json = loadExtJarJsonFile(ScreenConstant.SHEETTYPEDATAFILE_PATH);
        initSheetTypeDataList(json);
    }

    //初始化list
    private static void initSheetTypeDataList(JSONObject json) {
        if (json != null) {
            JSONArray jsonArry = json.getJSONArray("sheets");
            if (jsonArry != null && jsonArry.size() > 0) {
                for (int i = 0; i < jsonArry.size(); i++) {
                    JSONObject j = (JSONObject) jsonArry.get(i);
                    SheetTypeData sheetTypeData = new SheetTypeData();
                    sheetTypeData.setType(j.getString("type"));
                    sheetTypeData.setName(j.getString("name"));
                    sheetTypeData.setSort(j.getInteger("sort"));
                    sheetTypeDataList.add(sheetTypeData);
                }
            }
        }
    }

    public static List<SheetTypeData> getSheetTypeDataList() {
        if (sheetTypeDataList == null) {
            init();
        }
        return sheetTypeDataList;
    }

    public static void setSheetTypeDataList(List<SheetTypeData> sheetTypeDataList) {
        JSONArray arr = new JSONArray();
        for (int i = 0; i < sheetTypeDataList.size(); i++) {
            arr.set(i, sheetTypeDataList.get(i));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sheets", arr);
        try {
            setExtJarJsonFile(ScreenConstant.SHEETTYPEDATAFILE_PATH, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setList(List<SheetTypeData> list) {
        sheetTypeDataList = list;
    }

}
